In this demonstration,
you will learn how to create a microservice
in Node.js. First, take a movie scene. Nowadays, the OTT platform is very popular in the market. You will develop an app that helps the user get
registered in the movie app. After that, the user
authentication service will help the user get authenticated before using any
content from the app, like a movie video. The demo is divided
into four parts. The first part discusses the development of the
user movie service. The second part discusses the development of a user
authentication service. The third part shows the
microservice architecture where you will implement a discovery
service by using Eureka. The fourth part shows
the implementation of the API gateway through which all the service requests
will be diverted. Begin with the first part of creating a user movie service. Create a workspace folder and name the folder "Movie App". Open the folder by using the Visual Studio Code editor with the root folder Movie App. Before that, install
the stable version of Node.js and the Node
Package Manager or NPM on your system. To ensure the installation, open the integrated
terminal inside the Visual Studio Code and
give the commands node -v and npm -v. The commands node -v and npm -v commands check whether the node and the npm
are installed or not. You will also get the version
of the node and the npm. Here, the node version is 16.13.1 and the npm
version is 8.1.2. Both are stable versions
as of today's date. Now, start with the
project implementation. Create a folder with the
name "UserMovieService". Set up a local
environment for files and dependencies as the
application runs on Node.js. Install all the
dependencies related to the Node.js application
and development. To get an understanding of
the required dependencies, navigate to the
user movie service and get into the
UserMovieService folder. Configure the node by using
the npm init command. The npm init command initializes the node
inside the application and implements the
node-related configuration by using the package.json, where you can specify the required configuration
at the basic level. The npm init command also acts as an entry point
configuration for the application. The npm init command asks for the package
name and description. Here, provides the input. Otherwise, the npm init
will take the default. Let's go with the default. The output version is 1.0.0. The entry point is index.js, which indicates that all the execution points
will be index.js. Also, specify some test
commands, but for now, don't give any
specifications as you will add them to
the package.json. In the same way, asks for the git repository, keywords, author, and license. Based on the input, you can see the
details of the data available inside the
package.json data. The output shows the name as a user movie service,
version as 1.0.0. Description that
have not specified. Main is the index.js, author, and license. The message will
prompt, "Is this OK?" Type "yes" and refresh
the environment. By default, the package.json gets added inside the
user movie service. Now, you will learn to install dependencies to
perform the testing. Install the Mocha
Chai for the testing. You will see the
entry of Mocha and Chai within the package.json, and also add dependencies. Give the command npm
install mocha chai --save. Mocha and Chai are the two dependencies that
help to perform the testing. Do the installation,
and explore the package.json file to see the two dependencies added
within the Chai and the Mocha inside the
package.json file. The node_module folder has been created containing all the
dependency-related files. The package-lock.json
file also got created. You will also see a few
more dependencies such as decrypt JS, express, JSON Web Token, mongoose, and superset that
are going to be added to be part of
the application. Now run npm install
bcrypt JS --save command, and press Enter to
add the dependency. To add express
dependency, run npm, install express --save command. Similarly, for JWT, run npm install jsonwebtoken
--save command. For MongoDB, run npm
install mongoose --save. For supporting
script-related dependency, run npm install superset --save. In the package.json file, dependencies with their
version start from line number 12 to
line number 18. You have seen that it
is important to do the dependency-related
implementation inside the application. To keep the file separate, implement files and folders
inside the application. Create a config, model, tests, app.js, and index.js file as the entry points for
the application. This will help to implement the microservice
in a better way. Inside the UserMovieService, create folders named config, model, and test. Create database.js file
inside the config folder, movie.js file inside
the model folder, and app.test.js file inside
the test folder. Thank you.